package com.dj.dormitory.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.dormitory.entity.User;
import com.dj.dormitory.entity.excel.UserDataExcel;
import com.dj.dormitory.mapper.UserMapper;
import com.dj.dormitory.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dj.dormitory.listner.UserDataExcelListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User login(User input_user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name", input_user.getUserName());
        wrapper.eq("type",input_user.getType());
        User user = this.baseMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public Page<User> getPartList(Map input_data) {
        QueryWrapper wrapper = new QueryWrapper();
        if(input_data.containsKey("userName")&&!StringUtils.isEmpty((String)input_data.get("userName"))){
            wrapper.like("user_name", input_data.get("userName"));
        }
        if(input_data.containsKey("type")&&!StringUtils.isEmpty((String)input_data.get("type"))){
            wrapper.eq("type",input_data.get("type"));
        }
        Integer cur = (Integer) input_data.get("page");
        Integer limit = (Integer) input_data.get("limit");
        Page<User> page = new Page<>(cur,limit);
        this.baseMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public boolean addUsers(MultipartFile file)  {
        try{
            InputStream in = file.getInputStream();
            EasyExcel.read(in, UserDataExcel.class, new UserDataExcelListener(this)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
